//
//  RealiseEffect.m
//  Boss
//
//  Created by 孙昕 on 16/1/14.
//  Copyright © 2016年 孙昕. All rights reserved.
//

#import "RealiseEffect.h"
#import "Header.h"
@import AudioToolbox;
NSMutableArray *arrPoint;
void (^blockComplete)();
UIImageView *viewClear;
static RealiseEffect* singleObj;
SystemSoundID soundId;
@implementation RealiseEffect
+(void)initialize
{
    UIWindow *win=[UIApplication sharedApplication].keyWindow;
    CGPoint center=win.center;
    NSInteger radius=(win.bounds.size.width-40)/2;
    arrPoint=[[NSMutableArray alloc] initWithCapacity:30];
    for(CGFloat angle=0;angle<M_PI*2;angle+=M_PI_4/2)
    {
        CGFloat x=radius*sin(angle);
        CGFloat y=radius*cos(angle);
        [arrPoint addObject:[NSValue valueWithCGPoint:CGPointMake(center.x-x, center.y-y)]];
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"effect" ofType:@"wav"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&soundId);
    }
}
+(void)show:(void (^)())block
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleObj=[[RealiseEffect alloc] init];
    });
    blockComplete=block;
    UIWindow *win=[UIApplication sharedApplication].keyWindow;
    viewClear=[[UIImageView alloc] initWithFrame:win.bounds];
    viewClear.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    [win addSubview:viewClear];
    CAShapeLayer *layer=[CAShapeLayer layer];
    CGPoint center=win.center;
    NSInteger radius=(win.bounds.size.width-40)/2;
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:[arrPoint[0] CGPointValue]];
    [path addLineToPoint:[arrPoint[6] CGPointValue]];
    [path addLineToPoint:[arrPoint[13] CGPointValue]];
    [path addLineToPoint:[arrPoint[3] CGPointValue]];
    [path addLineToPoint:[arrPoint[10] CGPointValue]];
    [path addLineToPoint:[arrPoint[0] CGPointValue]];
    [path addArcWithCenter:center radius:radius startAngle:-M_PI_2 endAngle:2*M_PI-M_PI_2 clockwise:YES];
    layer.path=path.CGPath;
    layer.strokeColor=[UIColor yellowColor].CGColor;
    layer.lineWidth =  10.0f;
    layer.lineCap = kCALineCapRound;
    layer.lineJoin = kCALineJoinRound;
    layer.fillColor=[UIColor clearColor].CGColor;
    [viewClear.layer addSublayer:layer];
    CALayer *gradinetLayer = [ CALayer  layer];
    CAGradientLayer  * gradLayer1 = [CAGradientLayer   layer ];
    gradLayer1. frame =  CGRectMake( center.x-radius-10,center.y-radius-10,2*(radius+10),2*(radius+10));
    [gradLayer1 setColors :[ NSArray  arrayWithObjects :( id )[[ UIColor  redColor ] CGColor ],( id )[ COL(251, 216, 96, 1) CGColor ],  nil ]];
    [gradLayer1 setLocations: @[@0.5, @0.7, @1  ]];
    [gradLayer1 setStartPoint :CGPointMake ( 0.5 ,  1 )];
    [gradLayer1 setEndPoint: CGPointMake( 0.5,  0)];
    [gradinetLayer addSublayer:gradLayer1];
    CAGradientLayer  * gradLayer2 = [CAGradientLayer   layer ];
    gradLayer2. frame = CGRectMake( center.x-radius-10,center.y-radius-10,2*(radius+10),2*(radius+10));
    gradLayer2.opacity=0.2;
    [gradLayer2 setColors :[ NSArray  arrayWithObjects :( id )[ [UIColor greenColor] CGColor ],( id )[COL(251, 216, 96, 1) CGColor ],  nil ]];
    [gradLayer2 setLocations: @[@0.5, @0.7, @1  ]];
    [gradLayer2 setStartPoint :CGPointMake ( 0.5 ,  1 )];
    [gradLayer2 setEndPoint: CGPointMake( 0.5,0)];
    [gradinetLayer addSublayer:gradLayer2];
    [gradinetLayer setMask:layer];
    [viewClear. layer  addSublayer:gradinetLayer];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @(0.0);
    animation.toValue = @(1.0);
    animation.duration = 2;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.delegate=singleObj;
    [layer addAnimation:animation forKey:@"line"];
    AudioServicesPlaySystemSound(soundId);
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [UIView animateWithDuration:1 animations:^{
        viewClear.transform=CGAffineTransformMakeRotation(-M_PI);
        viewClear.transform=CGAffineTransformScale(viewClear.transform, 0.01, 0.01);
        viewClear.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2, 100, 0, 0);
    } completion:^(BOOL finished) {
        if(blockComplete)
        {
            blockComplete();
        }
        [viewClear removeFromSuperview];
        viewClear=nil;
        blockComplete=nil;
    }];
}
@end












